package com.wy.service.impl;

import com.wy.dao.CodeDao;
import com.wy.pojo.Code;
import com.wy.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * description CodeServiceImpl
 * author wy
 * date 2021-09-02 21:48
 **/
@Service
public class CodeServiceImpl implements CodeService
{

    @Autowired
    private CodeDao codeDao;

    @Override
    public Code getCodeByEmail(String email)
    {
        Code code = new Code();
        code.setEmail(email);
        Example<Code> example = Example.of(code);
        List<Code> all = codeDao.findAll(example);
        all.sort(new Comparator<Code>()
        {
            @Override
            public int compare(Code o1, Code o2)
            {
                return o2.getExpiretime().compareTo(o1.getExpiretime());
            }
        });
        return all.get(0);
    }
}
